<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\facade\Db;

class DataContentPseudo extends AdminBase
{
    public function index()
    {
        $param = $this->request->param();
        $db = Db::name('data_content_pseudo'); 
        if(!empty($param['parent_typeid'])) {
            $db = $db->where('parent_typeid',$param['parent_typeid']);
            if(!empty($param['typeid'])){
                $db = $db->where('typeid',$param['typeid']);
            }
        }
        if(!empty($param['kw'])){
            $db = $db->where('title','like','%'.$param['kw'].'%');
        }
        $list = $db->order('id desc')->paginate(['list_rows'=> 100,'query' => $param]);
        $count = $list->total();
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $param['parent_typeid']
        ])->select()->toArray();
        return view('',['list'=>$list,'count'=>$count,'param'=>$param,'parent_typelist'=>$parent_typelist,'typelist'=>$typelist]);
    }

    public function add()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->post();
            if( empty($param['title']) ){
                $this->error('请填写标题');
            }
            if( empty($param['parent_typeid']) || empty($param['typeid']) ){
                $this->error('请填写类别和所属项目');
            }
            if( empty($param['project']) ){
                $this->error('请填写所属项目');
            }
            if( empty($param['content']) ){
                $this->error('内容不能为空');
            }

            $violation = checkViolation($param['content']);
            if(!empty($violation)){
                $this->error('内容包含违规词:'.$violation);
            }

            $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $param['username'] = session('admin_auth.username');
            $param['attr'] = getAttr($param['title']);

            $insertId = Db::name('data_content_pseudo')->insertGetId($param);

            if( $insertId ) {
                xn_add_admin_log('添加伪原创数据');
                $this->success('添加成功',url('index'));
            } else {
                $this->error('添加失败');
            }

        }
        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();

        return view('form',[
            'parent_typelist'=>$parent_typelist
        ]);
    }

    public function edit()
    {
        if( $this->request->isPost() ) {
            $param = $this->request->param();
            if( empty($param['title']) ){
                $this->error('请填写标题');
            }
            if( empty($param['parent_typeid']) || empty($param['typeid']) ){
                $this->error('请填写类别和所属项目');
            }
            if( empty($param['project']) ){
                $this->error('请填写所属项目');
            }
            if( empty($param['content']) ){
                $this->error('内容不能为空');
            }

            $violation = checkViolation($param['content']);
            if(!empty($violation)){
                $this->error('内容包含违规词:'.$violation);
            }
            
            $param['top_typeid'] = get_typepid($param['parent_typeid']);
            $result = Db::name('data_content_pseudo')->update($param);
            if( $result ) {
                xn_add_admin_log('修改伪原创数据');
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
        $id = $this->request->get('id');
        $data = Db::name('data_content_pseudo')->find($id);

        $parent_typelist = Db::name('data_type')->where('level',1)->select()->toArray();
        $typelist = Db::name('data_type')->where([
            'level' => 2,
            'pid' => $data['parent_typeid']
        ])->select()->toArray();

        return view('form',[
            'data'=>$data,
            'parent_typelist'=>$parent_typelist,
            'typelist'=>$typelist
        ]);
    }

    public function delete()
    {
        $id = intval($this->request->get('id'));
        !($id>0) && $this->error('参数错误');
        Db::name('data_content_pseudo')->delete($id);
        xn_add_admin_log('删除伪原创数据');
        $this->success('删除成功');
    }

    public function deleteAll(){
        $ids = $this->request->get('ids');
        ($ids=='') && $this->error('参数错误');
        $ids = explode(',',$ids);
        Db::name('data_content_pseudo')->delete($ids);
        xn_add_admin_log('批量删除伪原创数据');
        $this->success("删除成功");
    }

    //生成内容
    public function ajaxMakePseudo(){
        if( $this->request->isAjax() ){
            $param = $this->request->param();
            $make = new \app\common\lib\MakePseudo();
            $make->dalei = $param['dalei'];
            $make->xiaolei = $param['xiaolei'];
            $make->xiangmu = $param['xiangmu'];
            $make->parent_typeid = $param['parent_typeid'];
            $make->typeid = $param['typeid'];
            $make->keywords = $param['keywords'];
            $data = $make->run($param['content']);
            $data = str_replace('<div></div>','',$data);
            $data = str_replace('<p></p>','',$data);
            $data = str_replace('<p><br/></p>','',$data);
            if(empty($data)){
                return json([
                    'status' => 0,
                    'msg' => '没有数据'
                ]);
            }
            return json([
                'status' => 1,
                'msg' => '请求成功',
                'data' => $data
            ]);
        }
    }


}
